package com.bird.设计模式.创建型.单例模式.ThradLocal线程单例;

/**
 * @data 2022/3/1 11:51
 * @author: bird
 * @description: “线程单例”--
 * threadLocal 创建的单例对象可以保证 线程唯一 但是不能保证内存全局唯一。
 */
public class ThreadLocalInstance {


    // 初始化 threadLocal
    private static ThreadLocal<ThreadLocalInstance> threadLocal = new ThreadLocal<ThreadLocalInstance>() {
        // 初始化
        @Override
        protected ThreadLocalInstance initialValue() {
            return new ThreadLocalInstance();
        }
    };

    // 私有构造器
    private ThreadLocalInstance() {
    }

    // 获取当前线程的实例对象
    public static ThreadLocalInstance getInstance() {
        return threadLocal.get();
    }

}
